package com.ljfresh.customer.utils;

import okhttp3.*;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * Created by zhuosr on 2018/5/21.
 */
@SuppressWarnings("deprecation")
public class HttpClientUtil {

    public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
    public static final MediaType FORM = MediaType.get("application/x-www-form-urlencoded;charset=UTF-8");
    public static final MediaType FORM2312 = MediaType.get("application/x-www-form-urlencoded;charset=GB2312");
    private static OkHttpClient client;

    static {
        client = new OkHttpClient.Builder()
                .connectTimeout(100, TimeUnit.SECONDS)
                .writeTimeout(100, TimeUnit.SECONDS)
                .readTimeout(200, TimeUnit.SECONDS)
                .build();
    }

    public static String getApiJson(String url,String token) throws IOException {
//        return "";
        Request request = new Request.Builder()
                .url(url)
                .header("Authorization","Bearer "+token)
                .header("Content-Type","application/json")
                .build();
        Response response = client.newCall(request).execute();
        return response.body().string();
    }

    //api请求
    public static String postApiJson(String url, String param,String token) throws IOException {
//        return "";
        RequestBody body = RequestBody.create(JSON, param);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .header("Authorization","Bearer "+token)
                .build();
        Response response = client.newCall(request).execute();
        return response.body().string();
    }

    //api请求  json数据方式提交
    public static String getApiJsonForHttpsJsonType(String url, String param) throws IOException {
//        return "";
        RequestBody body = RequestBody.create(JSON, param);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Response response = client.newCall(request).execute();
        return response.body().string();
    }

    //api请求   form数据方式提交
    public static String getApiJsonForHttpsFormType(String url, String param) throws IOException {
//        return "";
        RequestBody body = RequestBody.create(FORM, param);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Response response = client.newCall(request).execute();
        return response.body().string();
    }

    //api请求  GB2312编码  凌凯短信用
    public static String getApiJsonForGB2312(String url, String param) {
//        return "";
        RequestBody body = RequestBody.create(FORM2312, param);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Response response = null;
        try {
            response = client.newCall(request).execute();
            return response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}

